package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author tantao
 * @create 2020-04-11 23:46
 * @desc
 **/
@RestController
@Slf4j
public class OrderController {
    //public static final String PAYMENT_URL = "http://localhost:8001";
    /**
     * 使用服务名进行远程调用
     */
    public static final String PAYMENT_URL="http://CLOUD-PAYMENT-SERVICE";

    /**
     * RestTemplate是Spring用于同步client端的核心类，简化了与http服务的通信，并满足RestFul原则
     */
    @Autowired
    private RestTemplate restTemplate;


    /**
     * consumer-order 调用 provider-payment
     * @param payment
     * @return
     */
    @PostMapping("/consumer/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment) {
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymet(@PathVariable("id") Long id) {
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }
}
